import GameObject from "./GameObject";
import Utils from "./utils"
import GameConst from "./GameConst";
export default class AnimationObject extends GameObject{
    constructor(t, n) {
        super();
        if (typeof n === "undefined") {
            n = null;
        }
        
        this.frameTime = 1 / 30;
        this.animPos = new Laya.Point();
        this.currentFrame = 0;
        this.isPlaying = false;
        this.animationPower = 1;
        this.animationSpeed = 1;
        this.stopSpeed = 0;
        this.resetFrame = -1;
        this.resetStopFrame = false;
        this.destroyOnReset = false;
        this.playAnimationIn = -1;
        this.animationToPlay = 0;
        this.handlePos = false;
        this.initSpritePos = Utils.cjp(0, 0);
        this.stopFrame = -1;
        if (n) {
            this.sprite = n;
            this.initSpritePos = Utils.cjp(n.x, n.y);
        }
        this.animationFrames = t;
        this.animationLen = t.length;
    }

    onStop(){

    }
}

    AnimationObject.prototype.initAnim = function () {
        this.animPos.x = this.animPos.y = 0;
        this.animationPower = 1;
        this.animationSpeed = 1;
    };
    AnimationObject.prototype.setFrame = function (idx, t) {
        if (typeof t === "undefined") {
            t = true;
        }
        
        var n = this.animationLen;
        var frame = idx < 0 || idx >= n ? null : this.animationFrames[idx];
        var nextFrame = idx + 1 < 0 || idx + 1 >= n ? null : this.animationFrames[idx + 1];
        var frameLen = frame == null ? 0 : frame.length;
        var nextLen = nextFrame == null ? 0 : nextFrame.length;
        var stop = frame == null || frameLen <= 0;
        var a = !stop && nextFrame != null && nextLen > 0;
        var f = this.currentFrame - idx;
        var l = this.animationPower;
        if (stop) {
            this.onStop();
            if (this.resetFrame > 0) {
                this.setFrame(this.resetFrame);
                this.isPlaying = false;
                return;
            } else if (this.resetStopFrame) {
                this.setFrame(idx - 1);
                this.isPlaying = false;
                return;
            } else if (this.destroyOnReset) {
                this.destroy();
                return;
            }
            this.animPos.x = 0;
            this.animPos.y = 0;
            this.sprite.scaleX = 1;
            this.sprite.scaleY = 1;
            this.sprite.skewX = 0;
            this.sprite.skewY = 0;
            this.sprite.alpha = 1;
        } else if (t) {
            var c = !a;
            this.animPos.x = GameConst.TILE_SIZE_FACTOR * (c ? frame[0] * l : l * (frame[0] + f * (nextFrame[0] - frame[0])));
            this.animPos.y = GameConst.TILE_SIZE_FACTOR * (c ? frame[1] * l : l * (frame[1] + f * (nextFrame[1] - frame[1])));
            this.sprite.scaleX = c ? 1 + (frame[2] - 1) * l : 1 + (frame[2] + f * (nextFrame[2] - frame[2]) - 1) * l;
            this.sprite.scaleY = c ? 1 + (frame[3] - 1) * l : 1 + (frame[3] + f * (nextFrame[3] - frame[3]) - 1) * l;
            this.sprite.skewX = c ? frame[4] * l : l * (frame[4] + f * (nextFrame[4] - frame[4]));
            this.sprite.skewY = c ? frame[5] * l : l * (frame[5] + f * (nextFrame[5] - frame[5]));
            if (frameLen >= 7 && nextLen >= 7)
                this.sprite.alpha = c ? 1 + (frame[6] - 1) * l : 1 + (frame[6] + f * (nextFrame[6] - frame[6]) - 1) * l;
        }
        if (stop) {
            this.isPlaying = false;
            this.currentFrame = 0;
        }
    };
    AnimationObject.prototype.playAnimation = function (frame, t, n) {
        if (typeof t === "undefined") {
            t = 1;
        }
        if (typeof n === "undefined") {
            n = 1;
        }
        this.animationPower = t;
        this.animationSpeed = n;
        this.currentFrame = frame;
        this.isPlaying = true;
        //this.sprite.play(0);
    };
    AnimationObject.prototype.prepareToPlayAnimation = function (e, frame) {
        this.playAnimationIn = e;
        this.animationToPlay = frame;
    };
    AnimationObject.prototype.update = function (e) {
        if (this.isDestroyed)
            return;
        if (this.sprite) {
            if (this.isPlaying) {
                var t = e * this.animationSpeed;
                while (t > 0 && (this.stopFrame < 0 || ~~this.currentFrame != this.stopFrame) && this.isPlaying) {
                    this.currentFrame += t >= this.frameTime ? 1 : t / this.frameTime;
                    t -= this.frameTime;
                    this.setFrame(Math.floor(this.currentFrame), t <= this.frameTime);
                }
            }
            if (this.isDestroyed)
                return;
            if (this.playAnimationIn > 0) {
                this.playAnimationIn -= e;
                if (this.playAnimationIn <= 0) {
                    this.sprite.visible = true;
                    this.playAnimation(this.animationToPlay, 1, 1);
                }
            }
        }
        if (this.handlePos) {
            this.sprite.x = this.initSpritePos.x + this.animPos.x;
            this.sprite.y = this.initSpritePos.y + this.animPos.y;
        }
    };
